package com.foreveredu.erlms.web.struts.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.erlms.domain.News;
import com.foreveredu.erlms.domain.PageModel;
import com.foreveredu.erlms.domain.Role;
import com.foreveredu.erlms.domain.Teacher;
import com.foreveredu.erlms.domain.User;
import com.foreveredu.erlms.service.IManagementService;
import com.foreveredu.erlms.web.struts.actionform.NewsActionForm;

public class NewsAdminAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}
	
	// 保存修改的公告信息
	public ActionForward updateNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		News news = managementService.findNewsById(newsform.getId());
		news.setNewstopic(newsform.getNewstopic());
		news.setNewscontent(newsform.getFckContent());
		
		SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");
		news.setUpdatetime(time.format(new Date()));

		managementService.updateNews(news);
		return mapping.findForward("updateNewsSave");
	}
	
	// 跳转到增加公告页面
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("newsToAdd");
	}

	// 保存公告
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		News news = new News();
		newsform.setNewscontent(newsform.getFckContent());
		PropertyUtils.copyProperties(news, newsform);
		//添加新闻的表单校验
		if(newsform.getNewstopic()==null ||"".equals(newsform.getNewstopic())){
			request.setAttribute("news", news);
			request.setAttribute("newsTopicError", "请输入公告标题！");
			return mapping.findForward("newsToAdd");
		}
		if(newsform.getFckContent() ==null || "".equals(newsform.getFckContent())){
			request.setAttribute("news", news);
			request.setAttribute("newsContentError", "请输入公告内容！");
			return mapping.findForward("newsToAdd");
		}
		SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");
		news.setNewstime(time.format(new Date()));
		news.setState(0);
		managementService.saveNews(news);
		return mapping.findForward("save");
	}

	// 管理公告列表
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int offset = 0;
		String pageOffset = request.getParameter("pager.offset");// 获得翻页参数，即下一开始的纪录位置
		if (pageOffset != null && !pageOffset.equals("")) {
			offset = Integer.parseInt(pageOffset);
		}
		if(offset < 0){
			return mapping.findForward("newsList");
		}
		PageModel pm = managementService.findNewsList(offset);
		request.setAttribute("pm", pm);
		return mapping.findForward("newsList");
	}

	// 修改公告是否首页显示
	public ActionForward updateNewsState(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		News news = managementService.findNewsById(newsform.getId());
		if (news.getState() == 1) {
			news.setState(0);
			managementService.updateNewsStateById(news);
			return mapping.findForward("updateNewsState");
		}
		if (news.getState() == 0) {
			News stateNews = managementService.findNewsByState();
			if (stateNews != null && !"".equals(stateNews)) {
				stateNews.setState(0);
				managementService.updateNewsStateById(stateNews);
				news.setState(1);
				managementService.updateNewsStateById(news);
				return mapping.findForward("updateNewsState");
			}
			news.setState(1);
			managementService.updateNewsStateById(news);
			return mapping.findForward("updateNewsState");
		}
		return mapping.findForward("updateNewsState");
	}
	
	//删除公告
	public ActionForward deleteNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		managementService.deleteNewsById(newsform.getId());
		return mapping.findForward("deleteNews");
	}
	
	// 跳转到修改公告页面
	public ActionForward toUpdateNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		News news = managementService.findNewsById(newsform.getId());
		request.setAttribute("news", news);
		return mapping.findForward("toUpdateNews");
	}
	
	// 通过ID查找公告详情
	public ActionForward findNewsById(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		News news = managementService.findNewsById(newsform.getId());
		request.setAttribute("news", news);
		news.setTotal(news.getTotal()+1);
		managementService.updateNews(news);
		return mapping.findForward("findNewsById");
	}
	
}
